import auctionhaus.Listing
import auctionhaus.Customer

class BootStrap {
    def defaultCustomer
    def listing1
    def listing2
    def listing3
    def listing4
    def listing5
    def listing6
    def listing7

    def init = { servletContext ->
        if(!Customer.count()) {
            defaultCustomer = new Customer(email: "johnqpublic@acme.com", password: "asdfwe").save(failOnError: true)
        }
        if(!Listing.count()) {
            listing1 = new Listing(name: "Listing 1",
                    endTime: new Date()+1,
                    description: "blah blah blah",
                    seller: defaultCustomer,
                    startBid: 5).save(failOnError: true)
            listing1.dateCreated = new Date() - 5
            listing1.save(failOnError: true)

            listing2 = new Listing(name: "Listing 2",
                    endTime: new Date()+2,
                    description: "blah blah blah",
                    seller: defaultCustomer,
                    startBid: 5).save(failOnError: true)

            listing3 = new Listing(name: "Listing 3",
                    endTime: new Date()+3,
                    description: "blah blah blah",
                    seller: defaultCustomer,
                    startBid: 5).save(failOnError: true)
            listing3.dateCreated = new Date() - 4
            listing3.save(failOnError: true)

            listing4 = new Listing(name: "Listing 4",
                    endTime: new Date()+4,
                    description: "blah blah blah",
                    seller: defaultCustomer,
                    startBid: 5).save(failOnError: true)
            listing4.dateCreated = new Date() - 2
            listing4.save(failOnError: true)

            //Attempt to test requirement M-4 while still passing the Listing endDate constraints
            def testDate = new Date()
            testDate.seconds = testDate.seconds + 15
            listing5 = new Listing(name: "Listing 5",
                    endTime: testDate,
                    description: "blah blah blah",
                    seller: defaultCustomer,
                    startBid: 5).save(failOnError: true)

            listing6 = new Listing(name: "Listing 6",
                    endTime: new Date()+6,
                    description: "blah blah blah",
                    seller: defaultCustomer,
                    startBid: 5).save(failOnError: true)

            listing7 = new Listing(name: "Listing 7",
                    endTime: new Date()+7,
                    description: "blah blah blah",
                    seller: defaultCustomer,
                    startBid: 5).save(failOnError: true)
            listing7.dateCreated = new Date() - 1
            listing7.save(failOnError: true)
        }
    }
    def destroy = {
    }
}
